package com.seis_645.appTesting;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class CarInfoActivity extends Activity {

	private CarDataSource datasource;
	List<SpinnerInfo> spinners;
	private EditText make, model, pkage, year, gmHwy, gmCity, price;
	int accessories, driveability, comfort, appearance, safety;
	private Car car;

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		View viewToLoad = LayoutInflater.from(this.getParent()).inflate(
				R.layout.add_car, null);
		this.setContentView(viewToLoad);
		datasource = new CarDataSource(this);
		datasource.open();
		Intent intent = getIntent();
		long id = intent.getLongExtra("car_id", 0);
		System.out.println(Integer.toString((int) id));
		make = (EditText) findViewById(R.id.make);
		model = (EditText) findViewById(R.id.model);
		pkage = (EditText) findViewById(R.id.pkage);
		year = (EditText) findViewById(R.id.year);
		gmHwy = (EditText) findViewById(R.id.gmHwy);
		gmCity = (EditText) findViewById(R.id.gmCity);
		price = (EditText) findViewById(R.id.price);

		car = datasource.get_car(id);

		make.setText(car.get_make());
		model.setText(car.get_model());
		pkage.setText(car.get_pkage());
		year.setText(Integer.toString(car.get_year()));
		gmHwy.setText(Integer.toString(car.get_gmHwy()));
		gmCity.setText(Integer.toString(car.get_gmCity()));
		price.setText(Integer.toString((int) car.get_price()));

		spinners = new ArrayList<SpinnerInfo>();
		spinners.add(new SpinnerInfo((Spinner) findViewById(R.id.accSpinner),
				car.get_accessories()));
		spinners.add(new SpinnerInfo((Spinner) findViewById(R.id.driveSpinner),
				car.get_driveablility()));
		spinners.add(new SpinnerInfo(
				(Spinner) findViewById(R.id.comfortSpinner), car
						.get_comfort()));
		spinners.add(new SpinnerInfo(
				(Spinner) findViewById(R.id.appearanceSpinner), car
						.get_apperance()));
		spinners.add(new SpinnerInfo(
				(Spinner) findViewById(R.id.safetySpinner), car
						.get_accessories()));
		System.out.println(Integer.toString(car.get_driveablility()) + " " + Integer.toString(car.get_accessories()) + " " + Integer.toString(car.get_accessories()) + " ");
		for (int i = 0; i < spinners.size(); i++) {
			if (i == 4) {
				fillSpinner(spinners.get(i).get_spinner(),
						R.array.safety_array, spinners.get(i).get_selection());
			} else {
				fillSpinner(spinners.get(i).get_spinner(),
						R.array.ratings_array, spinners.get(i).get_selection());
			}
		}
		Button cancel = (Button) findViewById(R.id.btn_cancel_car);
		Button save = (Button) findViewById(R.id.btn_save_car);
		OnTouchListener cancelListener = new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_UP) {
					finish();
					// return true;
				}
				return false;
			}
		};

		OnTouchListener saveListener = new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_UP) {
					addToDatabase();
					finish();
				}
				return false;
			}
		};

		cancel.setOnTouchListener(cancelListener);
		save.setOnTouchListener(saveListener);

	}

	private void addToDatabase() {
		int value;
		for (int i = 0; i < spinners.size(); i++) {
			String compare = spinners.get(i).get_spinner().getSelectedItem().toString();
			if (compare.equalsIgnoreCase("excellent")
					|| compare.equalsIgnoreCase("five")) {
				value = 5;
			} else if (compare.equalsIgnoreCase("very good")
					|| compare.equalsIgnoreCase("four")) {
				value = 4;
			} else if (compare.equalsIgnoreCase("good")
					|| compare.equalsIgnoreCase("three")) {
				value = 3;
			} else if (compare.equalsIgnoreCase("fair")
					|| compare.equalsIgnoreCase("two")) {
				value = 2;
			} else {
				value = 1;
			}
			switch (i) {
			case 0:
				accessories = value;
				break;
			case 1:
				driveability = value;
				break;
			case 2:
				comfort = value;
				break;
			case 3:
				appearance = value;
				break;
			case 4:
				safety = value;
				break;
			}
		}

		System.out.println(year.getText().toString() + " "
				+ make.getText().toString() + " " + model.getText().toString()
				+ " " + pkage.getText().toString());
		System.out.println(gmHwy.getText().toString() + " "
				+ gmCity.getText().toString());
		System.out
				.println(Integer.toString(accessories) + " "
						+ Integer.toString(driveability) + " "
						+ Integer.toString(comfort) + " "
						+ Integer.toString(appearance) + " "
						+ Integer.toString(safety));

		datasource.open();

		datasource.update_car(car.get_id(), make.getText().toString(), model
				.getText().toString(), Integer.parseInt(year.getText()
				.toString()), pkage.getText().toString(), Integer
				.parseInt(gmHwy.getText().toString()), Integer.parseInt(gmCity
				.getText().toString()), Integer.parseInt(price.getText()
				.toString()), driveability, comfort, appearance, safety,
				accessories);

		datasource.close();

	}

	private void fillSpinner(Spinner spinner, int res, int selection) {
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
				this, res, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adapter);
		System.out.println(Integer.toString(selection));
		spinner.setSelection(5-selection);
	}

	@Override
	public void onPause() {
		datasource.close();
		super.onPause();
	}

	@Override
	public void onDestroy() {
		datasource.close();
		super.onDestroy();
	}

}
